﻿using System;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class NewCrossLink : Form
    {
        private Model model;
        private string chname,chrom_path;
        private int amount;
        private bool b = false;
        public NewCrossLink(Model m)
        {
            InitializeComponent();
            model = m;
        }
        public NewCrossLink(Model m,string n,string path,int i)
        {
            InitializeComponent();
            model = m;
            chrom_path = path;
            amount = i;
            chname = n;
            b = true;
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            if ((chrom_t_box.SelectedItem==null)&&(chrom_t_box.Text == ""))
            {
                MessageBox.Show("Please, choose chromosome table.");
                return;
            }
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Links\\CrossingoverLinks\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A crossingover link with this name already exists. Please, choose another name.");
                return;
            }
            ChromosomeTable chrom_t =null;
            if (!b)
            {
                foreach (ChromosomeTable t in model.model_chromosome_tables.Values)
                {
                    if ((string)chrom_t_box.SelectedItem == t.name)
                    {
                        chrom_t = t;
                        chrom_path = chrom_t.path;
                        amount = chrom_t.chromosome_pairs_amount;
                        break;
                    }
                }
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Links\\CrossingoverLinks\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("link");
            writer.WriteAttributeString("Type", "crossingover");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("PathToChromosomeTable", chrom_path);            
            writer.WriteAttributeString("ChromosomePairsAmount", amount.ToString());
            if (b)
            {
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteStartElement("chromosome");
                    writer.WriteAttributeString("Id", i.ToString());
                    writer.WriteStartElement("Probalities");
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            else
            {
                foreach (ChromosomeType chrom in chrom_t.table)
                {
                    writer.WriteStartElement("chromosome");
                    writer.WriteAttributeString("Id", chrom.id.ToString());
                    writer.WriteStartElement("Probalities");
                    for (int i = 0; i < chrom.amount_locuses; i++)
                    {
                        writer.WriteString("100");
                        if (i != chrom.amount_locuses - 1)
                            writer.WriteString(" ");
                    }
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("link");
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Links\\CrossingoverLinks\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("links")[0].Attributes[4]);
            doc.Increment(doc.DocumentElement.Attributes[2]);
            doc.GetElementsByTagName("crossingover_links")[0].AppendChild(el);
            doc.Save(model.model_path);
            Functions.add_node_func(new CrossingoverLink("file_input\\" + model.model_name + "\\Links\\CrossingoverLinks\\" + name_box.Text + ".xml"), name_box.Text, "crossl");
            this.Close();
        }

        private void NewCrossLink_Load(object sender, EventArgs e)
        {
            if (!b)
            {
                foreach (ChromosomeTable t in model.model_chromosome_tables.Values)
                {
                    chrom_t_box.Items.Add(t.name);
                }
            }
            else
            {
                chrom_t_box.Text = chname;
                chrom_t_box.Enabled = false;
            }
        }
    }
}
